/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PedidoUI.java
 *
 * Created on 30/06/2009, 01:01:42 AM
 */

package CRM_UI;

import DAO.DAOCliente;
import DAO.DAOEmpleado;
import DAO.DAOOrdenDeEntrega;
import DAO.DAOPedido;
import DAO.DAOProducto;
import DAO.DAOServicio;
import LogicaNegocio.Empleado;
import LogicaNegocio.OrdenDeEntrega;
import LogicaNegocio.Pedido;
import LogicaNegocio.Producto;
import LogicaNegocio.Servicio;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author juancate
 */
public class PedidoUI extends javax.swing.JFrame {

    String cliente;
    String ccEmpleado;
    String nuevoCodigo;
    Vector<Producto> losProductos;
    Vector<Servicio> losServicios;
    /** Creates new form PedidoUI */
    public PedidoUI(String cc) {
        initComponents();
        ccEmpleado = cc;
        panelArticulo.setVisible(false);
        pack();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelPedido = new javax.swing.JPanel();
        labelCliente = new javax.swing.JLabel();
        tefCliente = new javax.swing.JTextField();
        labelDireccion = new javax.swing.JLabel();
        tefDireccion = new javax.swing.JTextField();
        labelTelefono = new javax.swing.JLabel();
        tefTelefono = new javax.swing.JTextField();
        botSolicitarPedido = new javax.swing.JButton();
        panelArticulo = new javax.swing.JPanel();
        labelTipo = new javax.swing.JLabel();
        cbTipoArticulo = new javax.swing.JComboBox();
        labelArticulo = new javax.swing.JLabel();
        cbNombreArticulo = new javax.swing.JComboBox();
        botAceptarPedido = new javax.swing.JButton();
        botAgregarArticulo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Pedido");

        panelPedido.setBorder(javax.swing.BorderFactory.createTitledBorder("Realizar Pedido"));

        labelCliente.setText("Cliente:");

        labelDireccion.setText("Dirección:");

        labelTelefono.setText("Teléfono:");

        botSolicitarPedido.setText("Solicitar Pedido");
        botSolicitarPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botSolicitarPedidoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelPedidoLayout = new javax.swing.GroupLayout(panelPedido);
        panelPedido.setLayout(panelPedidoLayout);
        panelPedidoLayout.setHorizontalGroup(
            panelPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPedidoLayout.createSequentialGroup()
                .addContainerGap(111, Short.MAX_VALUE)
                .addGroup(panelPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPedidoLayout.createSequentialGroup()
                        .addComponent(labelDireccion)
                        .addGap(12, 12, 12)
                        .addComponent(tefDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPedidoLayout.createSequentialGroup()
                        .addComponent(labelTelefono)
                        .addGap(12, 12, 12)
                        .addComponent(tefTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPedidoLayout.createSequentialGroup()
                        .addComponent(labelCliente)
                        .addGap(12, 12, 12)
                        .addComponent(tefCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(55, 55, 55))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPedidoLayout.createSequentialGroup()
                .addGap(156, 156, 156)
                .addComponent(botSolicitarPedido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(140, 140, 140))
        );
        panelPedidoLayout.setVerticalGroup(
            panelPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPedidoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tefCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tefDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelDireccion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPedidoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tefTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelTelefono))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botSolicitarPedido)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        panelArticulo.setBorder(javax.swing.BorderFactory.createTitledBorder("Agregar articulo"));

        labelTipo.setText("Tipo de articulo:");

        cbTipoArticulo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Producto", "Servicio" }));
        cbTipoArticulo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTipoArticuloActionPerformed(evt);
            }
        });

        labelArticulo.setText("Producto:");

        botAceptarPedido.setText("Aceptar");
        botAceptarPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botAceptarPedidoActionPerformed(evt);
            }
        });

        botAgregarArticulo.setText("Agregar");
        botAgregarArticulo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botAgregarArticuloActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelArticuloLayout = new javax.swing.GroupLayout(panelArticulo);
        panelArticulo.setLayout(panelArticuloLayout);
        panelArticuloLayout.setHorizontalGroup(
            panelArticuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelArticuloLayout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addGroup(panelArticuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelTipo)
                    .addComponent(labelArticulo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelArticuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbTipoArticulo, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbNombreArticulo, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(25, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelArticuloLayout.createSequentialGroup()
                .addContainerGap(133, Short.MAX_VALUE)
                .addComponent(botAceptarPedido)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botAgregarArticulo)
                .addGap(123, 123, 123))
        );
        panelArticuloLayout.setVerticalGroup(
            panelArticuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelArticuloLayout.createSequentialGroup()
                .addGroup(panelArticuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTipo)
                    .addComponent(cbTipoArticulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelArticuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelArticulo)
                    .addComponent(cbNombreArticulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(panelArticuloLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botAceptarPedido)
                    .addComponent(botAgregarArticulo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panelArticulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelPedido, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelArticulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botSolicitarPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botSolicitarPedidoActionPerformed
        DAOOrdenDeEntrega dao = new DAOOrdenDeEntrega();
        String ultimoCodigo = dao.consultarMayorCodigo();
        int numeroNuevoCodigo = Integer.parseInt(ultimoCodigo.substring(2));
        numeroNuevoCodigo += 1;
        nuevoCodigo = "OE";
        if(numeroNuevoCodigo > 99)
            nuevoCodigo += "0";
        else if(numeroNuevoCodigo > 9)
            nuevoCodigo += "";
        else
            nuevoCodigo += "00";
        nuevoCodigo += numeroNuevoCodigo;

        cliente = tefCliente.getText();
        String direccion = tefDireccion.getText();
        String telefono = tefTelefono.getText();

        OrdenDeEntrega orden = new OrdenDeEntrega(nuevoCodigo, "Iniciada",direccion, telefono, ccEmpleado);
        
        int resultado = dao.guardarOrdenDeEntrega(orden);
        if(resultado != -1) {
            panelArticulo.setVisible(true);
            pack();
        }
        else {
            JOptionPane.showMessageDialog(this, "Error en los datos.", "Error", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_botSolicitarPedidoActionPerformed

    private void cbTipoArticuloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTipoArticuloActionPerformed
        String itemEscogido = (String)cbTipoArticulo.getSelectedItem();
        if(itemEscogido.equals("Producto")) {
            labelArticulo.setText("Producto:");
            cbNombreArticulo.removeAllItems();
            DAOProducto dao = new DAOProducto();
            losProductos = dao.consultarProductos();
            for(int i = 0; i < losProductos.size(); i++) {
                cbNombreArticulo.insertItemAt(losProductos.elementAt(i).getNombreArticulo(), i);
            }
        }
        else {
            labelArticulo.setText("Servicio:");
            cbNombreArticulo.removeAllItems();
            DAOServicio dao = new DAOServicio();
            losServicios = dao.consultarServicios();
            for(int i = 0; i < losServicios.size(); i++) {
                cbNombreArticulo.insertItemAt(losServicios.elementAt(i).getNombreArticulo(), i);
            }
        }
        cbNombreArticulo.setSelectedIndex(0);
    }//GEN-LAST:event_cbTipoArticuloActionPerformed

    private void botAgregarArticuloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botAgregarArticuloActionPerformed
        int articuloEscogido = cbNombreArticulo.getSelectedIndex();
        String itemEscogido = (String)cbTipoArticulo.getSelectedItem();
        DAOPedido dao = new DAOPedido();

        String codigoArticulo = "";
        if(itemEscogido.equals("Producto")) {
            codigoArticulo = losProductos.elementAt(articuloEscogido).getCodigoArticulo();
        }
        else {
            codigoArticulo = losServicios.elementAt(articuloEscogido).getCodigoArticulo();
        }
        Pedido p = new Pedido(cliente, nuevoCodigo, codigoArticulo);
        int resultado = dao.guardarPedido(p);

        if(resultado != -1) {
            JOptionPane.showMessageDialog(this, "Se insertó satisfactoriamente el articulo.", "Éxito", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, "No se insertó satisfactoriamente el articulo.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_botAgregarArticuloActionPerformed

    private void botAceptarPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botAceptarPedidoActionPerformed
        panelArticulo.setVisible(false);
        pack();

        DAOPedido dao = new DAOPedido();

        Vector<String[]> resultado = dao.consultarArticulos(nuevoCodigo);
        if(resultado != null) {
            DAOOrdenDeEntrega daoOE = new DAOOrdenDeEntrega();
            OrdenDeEntrega orden = daoOE.consultarOrdenEntrega(nuevoCodigo);

            String informe = "Codigo orden entrega: " + nuevoCodigo;
            informe += "\nCodigo cliente: "+ cliente;
            DAOCliente daoCli = new DAOCliente();
            informe += "\nNombre cliente: " + daoCli.consultarNombreCliente(cliente);
            informe += "\nEstado: "+ orden.getEstadoEntrega();
            DAOEmpleado daoEmpl = new DAOEmpleado();
            Empleado empl = daoEmpl.consultarEmpleado(ccEmpleado);
            informe += "\nCodigo del responsable: "+ ccEmpleado;
            informe += "\nNombre del responsable: "+ empl.getNombreEmpleado();
            informe += "\n\nCódigo Articulo.\t|Nombre Articulo\t\n";
            informe += "________________________________________________________________\n";

            for(int i = 0; i < resultado.size();i++) {
                informe += resultado.elementAt(i)[0]+"\t\t| "+resultado.elementAt(i)[0]+"\t\t\n";
            }

            JTextArea area = new JTextArea(30, 50);
            JScrollPane scroll = new JScrollPane(area);

            area.setText(informe);
            area.setEditable(false);

            JOptionPane.showMessageDialog(this, scroll, "Orden de Entrega", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_botAceptarPedidoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PedidoUI("").setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botAceptarPedido;
    private javax.swing.JButton botAgregarArticulo;
    private javax.swing.JButton botSolicitarPedido;
    private javax.swing.JComboBox cbNombreArticulo;
    private javax.swing.JComboBox cbTipoArticulo;
    private javax.swing.JLabel labelArticulo;
    private javax.swing.JLabel labelCliente;
    private javax.swing.JLabel labelDireccion;
    private javax.swing.JLabel labelTelefono;
    private javax.swing.JLabel labelTipo;
    private javax.swing.JPanel panelArticulo;
    private javax.swing.JPanel panelPedido;
    private javax.swing.JTextField tefCliente;
    private javax.swing.JTextField tefDireccion;
    private javax.swing.JTextField tefTelefono;
    // End of variables declaration//GEN-END:variables

}
